package com.kanshu.ksgb.fastread.doudou.base.baseui;

import android.os.Handler;
import android.os.Looper;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.LayoutInflater;
import android.view.View;

import com.kanshu.ksgb.fastread.doudou.R;
import com.kanshu.ksgb.fastread.doudou.app.constants.Constants;
import com.kanshu.ksgb.fastread.doudou.base.basemvp.IBannerView;
import com.kanshu.ksgb.fastread.doudou.common.net.bean.BaseResult;
import com.kanshu.ksgb.fastread.doudou.common.util.SwipeRefreshHelper;
import com.kanshu.ksgb.fastread.doudou.common.util.Utils;
import com.kanshu.ksgb.fastread.doudou.common.view.BannerLayout;
import com.kanshu.ksgb.fastread.doudou.common.view.EmptyLayout;
import com.kanshu.ksgb.fastread.doudou.module.book.bean.BannerItem;
import com.kanshu.ksgb.fastread.doudou.module.book.retrofit.requestparams.BannerRequestParams;
import com.kanshu.ksgb.fastread.doudou.common.business.utils.BookUtils;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by wcy on 2017\7\13 0013.
 */

public abstract class BannerListFragment<U> extends BaseListFragment<U> implements IBannerView<BaseResult<List<BannerItem>>> {
    protected BannerLayout mBannerLayout;
    protected List<BannerItem> mBannerItems;
    protected BannerRequestParams mBannerParams = new BannerRequestParams();
    @Override
    protected void init() {
        super.init();
        mEmptyLayout.setRetryListener(new EmptyLayout.OnRetryListener() {
            @Override
            public void onRetry() {
                showLoading(null);
                getData();
            }
        });
        SwipeRefreshHelper.init(mSwipeRefresh, new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                getData();
            }
        });
    }

    protected void getData() {
        getBannerAysnc(mBannerParams);
        mRequestParams.page = 1;
        getContentAysnc(mRequestParams);
    }

    @Override
    protected void initRecyclerView() {
        initBannerLayout();
        super.initRecyclerView();
    }

    @Override
    public void showBanner(BaseResult<List<BannerItem>> result) {
        if (BaseResult.isNotNull(result) && !Utils.isEmptyList(result.result.data)) {
            final List<BannerItem> list = result.result.data;
            mBannerItems = list;
            List<String> strs = new ArrayList<>();
            for (BannerItem bannerItem : list) {
                if (bannerItem.is_close == BannerItem.UN_CLOSED) {
                    strs.add(bannerItem.img_url);
                }
            }
            if (mEmptyLayout != null) {
                mEmptyLayout.hide();
            }
            mBannerLayout.setVisibility(View.VISIBLE);
            mBannerLayout.setViewUrls(strs);
            mBannerLayout.setOnBannerItemClickListener(new BannerLayout.OnBannerItemClickListener() {
                @Override
                public void onItemClick(int position) {
                    BookUtils.processJump(getActivity(), Integer.parseInt(list.get(position).link_type), list.get(position).url);
                }
            });
        } else {
            mBannerItems = null;
            hideBannerLayout();
            //displayNoData(Constants.ErrCode.NoData);
        }
    }

    private void hideBannerLayout() {
        Handler handler = new Handler(Looper.getMainLooper());
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                mBannerLayout.setVisibility(View.GONE);
            }
        }, 300);
    }

    @Override
    public void showBannerError(int code, String desc) {
        hideBannerLayout();
        mBannerItems = null;
        displayNoData(code);
    }

    @Override
    public void showContent(BaseResult<List<U>> listBaseResult) {
        if (mRequestParams.page == 1) {
            if (Utils.isEmptyList(listBaseResult.result.data)) {
                SwipeRefreshHelper.swipeRefreshCompleted(mSwipeRefresh, mAdapter);
                displayNoData(Constants.ErrCode.NoData);
                return;
            }
            mList.clear();
            mAdapter.setIsNoMoreData(false);
        }
        mList.addAll(listBaseResult.result.data);
        if (mEmptyLayout != null) {
            mEmptyLayout.hide();
        }
        SwipeRefreshHelper.swipeRefreshCompleted(mSwipeRefresh, mAdapter);
        if (mRequestParams.page >= listBaseResult.result.total_page) {
            mAdapter.noMoreData();
        } else {
            mRequestParams.page++;
        }
        mAdapter.notifyDataSetChanged();
    }

    @Override
    public void showError(int errCode, String errDesc) {
        SwipeRefreshHelper.swipeRefreshCompleted(mSwipeRefresh, mAdapter);
        displayNoData(errCode);
    }

    private void displayNoData(int errCode) {
        if (Utils.isEmptyList(mBannerItems) && Utils.isEmptyList(mList)) {
            showEmptyByCode(errCode);
        }
    }

    protected void initBannerLayout() {
        View v = LayoutInflater.from(getActivity())
                .inflate(R.layout.banner_layout, null);
        mBannerLayout = (BannerLayout) v.findViewById(R.id.banner);
        mAdapter.addHeaderView(v);
        mBannerLayout.setVisibility(View.GONE);
    }


    protected abstract void getBannerAysnc(BannerRequestParams params);
}
